/*
 *说明：NSCG信息表 首页脚本
 *
 *@author liuxun 2012-03-13
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'节点名称',field:'node_decode',align:'center',width:50},
            
            {title:'NSCG名称',field:'bastion_name',align:'center',width:80},

            {title:'NSCG IP',field:'bastion_ip',align:'center',width:80},
            
            {title:'NSCG 管理员',field:'bastion_admin',align:'center',width:80},
            
            {title:'NSCG 序列号',field:'bastion_number',align:'center',width:80},
            
            {title:'描述',field:'bastion_desc',align:'center',width:80}

        ]],
		pagination:true,
		singleSelect: false
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#addBtn').click(addOperation);
	$('#editBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'C_BASTION!queryList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//添加NSCG信息表
function addOperation(){  
	var url = 'C_BASTION!showAdd.do';
	openWin(url,'pop');
}

//编辑NSCG信息表
function editOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}

	var url = 'C_BASTION!showEdit.do?model.key=' + rows[0].key;
	openWin(url,'pop');
}   

//删除NSCG信息表
function deleteOperation() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'C_BASTION!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//NSCG IP地址校验
function checkNscg_ip(str){
	var reg = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
	if(!reg.test(str)){
		showInfoWin('NSCG IP地址格式不正确！');
		$('#bastion_ip').val('');
		return true;
	}
		return false;
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}